//==================================================
// D_NC_OW_scale.ino
// 2015-02-28
// Disable SID select. SID1 only
//==================================================
// Nabton Client Generic Configuration
//==================================================

#define NFLOAT 2  // No of decimals i float value
#define SIDN  3   // No of SIDs
#define SID1 61
#define SID2 62
#define SID3 63
#define SID4 904
#define SID5 905
#define SID6 906
#define SID7 907
#define SID8 908

#define MAX_SID 10
#define MAX_ORDERS 100
char g_msg[120];
int g_sids[10] = {SIDN,SID1,SID2,SID3,SID4,SID5,SID6,SID7,SID8};
#define PIN_LED_STATUS 13

// Arduino-RPi protocol
#define NABTON_DATA     1 
#define NABTON_DATA_ACK 2
#define NABTON_NODATA   3
#define NABTON_LOG      8 
#define NABTON_ERROR    9 

char g_errmsg[120];  // Error message
int  g_error = 0;     // Error flag
char g_logmsg[120];  // Log message
int  g_log = 0;       // Log flag

#define ERR_SID_NOT_FOUND  "SID_not_found"
#define ERR_UNSUPPORTED_MESSAGE "Unsupported_message"
#define ERR_NO_MESSAGE "No_message"
#define ERR_SID_MISMATCH "SID_Mismatch"
//=================================================
//
// D0 RX used for serial communication to server (Raspberry Pi)
// D1 TX used for serial communication to server (Raspberry Pi)
// D2 Interrupt (tone)
// D3 Message Pin (blink led)
// D4 Led Arm
// D5 Led SID1
// D6 Led SID2
// D7 Led SID3
// D8 
// D9 
// D10 Button 1  
// D11 Button 2 
// D12 Signal from scale
// D13
//
// A0
// A1
// A2
// A3
// A4 
// A5 
//=================================================
//==================================================
// Nabton Client Application Configuration
//==================================================

  // Big display
  int   zero_level  = 1290;
  float refKg       = 86.7;
  int   refValue    = 3030; 
  float noice_level = 0.1;
  
  // Model EKS ELECTRONIC Small display
  //int   zero_level  = 725;
  //float refKg       = 86.7;
  //int   refValue    = 2026; 
  //float noice_level = 0.2;
  #define N_FILTER 5
  #define N_LEDS 3

int g_led[10];
int cur;
int g_selected_sid;


int   g_sid;
float factor;
float oldTs,newTs;
int   value = HIGH;
int   nvalue = 0;
int   count = 0;
float oldw[10];
float sum,ftemp,weight = 0.0;
int ready = 0;
int sid[10];
 
#define PIN_LED_ARM   4
#define PIN_LED_MSG   3
#define PIN_INTERRUPT 2
#define PIN_SIGNAL   12
#define PIN_LED_SID1  5
#define PIN_LED_SID2  6
#define PIN_LED_SID3  7
#define PIN_BUTTON_1 10
#define PIN_BUTTON_2 11

//=================================================
void setLed(int led)
//=================================================
{
  for (int i=1;i<=N_LEDS;i++)
  {
    digitalWrite(g_led[i],LOW);
  }
  digitalWrite(g_led[led],HIGH);
  g_selected_sid = led;
}
//=================================================
int selectSid()
//=================================================
{
  cur = 1;
  while(digitalRead(PIN_BUTTON_1) == LOW)
  {
    int x2 = digitalRead(PIN_BUTTON_2);
    int par = x2;
    switch(par)
    {
     case 0 :
       break;
     case 1 :
       cur++;
       if(cur > N_LEDS)cur=1;
       setLed(cur);
       break;
     default :
      Serial.println("This will not happen\n" );
    }
    delay(150); 
  }
  return(cur);
}
//=================================================
void stamp()
//=================================================
{
  int i;
  float gtemp;
  value = digitalRead(PIN_SIGNAL);
  if(value == LOW)
  {
    count++;
  }
  if(count > 0 && value == HIGH)
  {
     nvalue++;
     for(i=N_FILTER;i>0;i--)oldw[i] = oldw[i-1];
     oldw[0]=(count-zero_level)*factor;
     ftemp = 0.0;
     sum = 0.0;
     for(i=0;i<N_FILTER;i++)
     {
       ftemp = ftemp + (oldw[i] - oldw[i+1])*(oldw[i] - oldw[i+1]);
     }
     for(i=0;i<=N_FILTER;i++)
     {
       sum = sum + oldw[i];
     }
     sum = sum/(N_FILTER+1);
     if(ftemp < noice_level && sum > 10.0 && sum < 150.)
     {
       weight = sum;
       sum = 0.0;
     }
     /*Serial.print(count);
     Serial.print("  ");
     Serial.print(noice_level);
     Serial.print("  SID=");
     Serial.print(g_sid);
     Serial.print("  ");
     Serial.print(ftemp);
     Serial.print("  ");
     Serial.println(sum);*/
     count = 0;
  }
}
//=================================================
void NB_printFloat(float value, int places) 
//=================================================
{
  int digit;
  float tens = 0.1;
  int tenscount = 0;
  int i;
  float tempfloat = value;
  float d = 0.5;
  
  if (value < 0) d *= -1.0;
  for (i = 0; i < places; i++) d/= 10.0;    
  tempfloat +=  d;
  if (value < 0) tempfloat *= -1.0;
  while ((tens * 10.0) <= tempfloat) 
  {
    tens *= 10.0;
    tenscount += 1;
  }
  if (value < 0) Serial.print('-');
  if (tenscount == 0) Serial.print(0, DEC);
  for (i=0; i< tenscount; i++) 
  {
    digit = (int) (tempfloat/tens);
    Serial.print(digit, DEC);
    tempfloat = tempfloat - ((float)digit * tens);
    tens /= 10.0;
  }
  if (places <= 0) return;
  Serial.print('.');  
  for (i = 0; i < places; i++) 
  {
    tempfloat *= 10.0; 
    digit = (int) tempfloat;
    Serial.print(digit,DEC);  
    tempfloat = tempfloat - (float) digit; 
  }
}

//=================================================
void NB_sendFloatData(int sid, float value)
//=================================================
{
    digitalWrite(PIN_LED_STATUS,HIGH);
    Serial.print(NABTON_DATA);Serial.print(" "); 
    Serial.print(sid);Serial.print(" "); 
    Serial.print(NFLOAT);Serial.print(" "); 
    NB_printFloat(value,NFLOAT);
    Serial.print(";");
    digitalWrite(PIN_LED_STATUS,LOW);
    //strcpy(dl[3],p);
} 
//=================================================
void setup()
//================================================= 
{
  int i;
  Serial.begin(9600);
  pinMode(PIN_LED_STATUS, OUTPUT);
  pinMode(PIN_SIGNAL,     INPUT);
  pinMode(PIN_LED_ARM,    OUTPUT);
  pinMode(PIN_LED_MSG,    OUTPUT);
  pinMode(PIN_INTERRUPT,  OUTPUT);
  pinMode(PIN_LED_SID1,   OUTPUT);
  pinMode(PIN_LED_SID2,   OUTPUT);
  pinMode(PIN_LED_SID3,   OUTPUT);
  pinMode(PIN_BUTTON_1,   INPUT);
  pinMode(PIN_BUTTON_2,   INPUT);
  
  digitalWrite(PIN_LED_SID1,HIGH);  delay(400);
  digitalWrite(PIN_LED_SID2,HIGH);  delay(400);
  digitalWrite(PIN_LED_SID3,HIGH);  delay(400);
  digitalWrite(PIN_LED_MSG,HIGH);   delay(400);
  digitalWrite(PIN_LED_ARM,HIGH);   delay(400);

  digitalWrite(PIN_LED_ARM,LOW);delay(400);
  digitalWrite(PIN_LED_MSG,LOW);delay(400);
  digitalWrite(PIN_LED_SID3,LOW);delay(400);
  digitalWrite(PIN_LED_SID2,LOW);delay(400);
  digitalWrite(PIN_LED_SID1,LOW);delay(400);
  
  g_led[1] = PIN_LED_SID1;
  g_led[2] = PIN_LED_SID2;
  g_led[3] = PIN_LED_SID3;  
  
  attachInterrupt(0, stamp, FALLING);
  for(i=N_FILTER;i>0;i--)oldw[i] =0.;  
  factor = refKg/(refValue-zero_level); 
  
  setLed(1);
  g_selected_sid = selectSid();
  tone(PIN_INTERRUPT, 32767);

}

//=================================================
void loop()
//=================================================
{
      digitalWrite(PIN_LED_ARM,HIGH);
      if(weight > 4.0)
      {
       int s = g_sids[g_selected_sid];
       NB_sendFloatData(s,weight);  
       //Serial.println(weight);
       digitalWrite(PIN_LED_MSG,HIGH);
       digitalWrite(PIN_LED_ARM,LOW);
       noTone(PIN_INTERRUPT);
       delay(10000);
       digitalWrite(PIN_LED_MSG,LOW);
       selectSid(); 
       weight = 0.0;
       nvalue = 0;
       tone(PIN_INTERRUPT, 32767);
     }
     
  if(digitalRead(11) == HIGH)setup();
  delay(150); 
}


